package com.nuoniu.sibanyun.controller.work;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.work.WorkReportUser;
import com.nuoniu.sibanyun.service.work.IWorkReportUserService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/work/reportUser")
@Api(tags = "报告管理")
public class WorkReportUserController extends BaseController {

    @Autowired
    private IWorkReportUserService reportUserService;

    /**
     * 获取报表设置详情
     * @author 小啊刚
     * @date 2020-09-11
     */
    @GetMapping("detail")
    public HttpResult detail(Integer workType) {
        UserInfo userInfo = UserThreadLocal.get();
        return HttpResult.ok(reportUserService.getByOfficeId(userInfo.getOfficeId(), workType));
    }

    /**
     * 保存报告设置
     * @author 小啊刚
     * @date 2020-09-11
     */
    @PostMapping("saveReportUser")
    public HttpResult saveReportUser(@RequestBody List<WorkReportUser> reportUserList) {
        UserInfo userInfo = UserThreadLocal.get();
        reportUserService.deleteByOffice(userInfo.getOfficeId(), reportUserList.get(0).getWorkType());
        for(WorkReportUser reportUser : reportUserList) {
            reportUser.setUserId(userInfo.getUserId());
            reportUser.setOfficeId(userInfo.getOfficeId());
            reportUserService.save(reportUser);
        }
        return HttpResult.ok();
    }

}
